<?php
class IndexAction extends CommonAction {
    public function index(){
		//1. 获取我们的access list
		$allAccessList = D('Access')->getAll(array('menu_group'=>array('gt',0)));
		
		//2. 查看用户权限
		$userAccessList = D('Role')->where(array('id'=>$this->adminInfo['role_id']))->getField('access_list');
		//3. 过滤用户拥有的菜单
		$userAccessArray = explode(',',$userAccessList);
		$accessListMenuId = array();
		foreach($allAccessList as $key=>$val){
			if($this->adminInfo['role_id'] == 0 || in_array($val['id'], $userAccessArray)){
				$accessListMenuId[] = $val['id'];	
			}
		}
		//4. 过滤权限菜单
		$accessListMenu = array();
		foreach($allAccessList as $key=>$val){
			if($val['menu'] == 1){
				$accessListMenu[$val['menu_group']]['level'] = $val;
			}elseif($val['menu'] == 2 && in_array($val['id'], $accessListMenuId)){
				$accessListMenu[$val['menu_group']]['tree'][] = $val;
			}
		}
		
		//5. 生成权限菜单
		$multisortMenu = array();
		foreach($accessListMenu as $key=>$val){
			if(empty($val['tree'])){
				unset($accessListMenu[$key]);	
				continue;
			}
			$multisortMenu[] = $val['level']['row_sort'];
		}
		array_multisort($multisortMenu,$accessListMenu);
		$this->assign('accessTree', array_values($accessListMenu));
		$this->display();
    }
}